package com.google.code.pdfbrick.engine;

import java.io.IOException;

import com.google.code.pdfbrick.engine.exception.PDFOperationException;
import com.google.code.pdfbrick.engine.exception.PageIndexOutOfBoundException;
import com.lowagie.text.pdf.BadPdfFormatException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfReader;

/**
 * @author Johny Huang
 * @date 2009-3-13
 */
public class PDFOperator {

	public void extract(PdfCopy desCopy, PdfReader srcReader, int startPage,
			int endPage) throws PDFOperationException {
		int pageSize = srcReader.getNumberOfPages();
		try {
			comparePageSize(startPage, pageSize);
			comparePageSize(endPage, pageSize);

			if (startPage > 0 && endPage > 0) {
				if (endPage >= startPage) {
					for (int i = startPage; i <= endPage; i++) {
						// Copy pages to desFile
						desCopy.addPage(desCopy.getImportedPage(srcReader, i));
					}
				} else {
					for (int i = startPage; i >= endPage; i--) {
						// Copy pages to desFile
						desCopy.addPage(desCopy.getImportedPage(srcReader, i));
					}
				}
			}
		} catch (IOException e) {
			throw new PDFOperationException(e.getMessage(), e);
		} catch (BadPdfFormatException e) {
			throw new PDFOperationException(e.getMessage(), e);
		} catch (PageIndexOutOfBoundException e) {
			throw new PDFOperationException(e.getMessage(), e);
		}

	}

	public void extractAll(PdfCopy desCopy, PdfReader srcReader)
			throws PDFOperationException {
		int pageSize = srcReader.getNumberOfPages();
		try {
			for (int i = 1; i <= pageSize; i++) {
				// Copy pages to desFile
				desCopy.addPage(desCopy.getImportedPage(srcReader, i));
			}
		} catch (IOException e) {
			throw new PDFOperationException(e.getMessage(), e);
		} catch (BadPdfFormatException e) {
			throw new PDFOperationException(e.getMessage(), e);
		}

	}

	public void extract(PdfCopy desCopy, PdfReader srcReader, int pageNumber)
			throws PDFOperationException {
		int pageSize = srcReader.getNumberOfPages();
		try {
			comparePageSize(pageNumber, pageSize);
			desCopy.addPage(desCopy.getImportedPage(srcReader, pageNumber));
		} catch (IOException e) {
			throw new PDFOperationException(e.getMessage(), e);
		} catch (BadPdfFormatException e) {
			throw new PDFOperationException(e.getMessage(), e);
		} catch (PageIndexOutOfBoundException e) {
			throw new PDFOperationException(e.getMessage(), e);
		}
	}

	public static int getPdfNumberOfPages(String filePath) {
		int size = 0;
		PdfReader reader = null;
		try {
			reader = new PdfReader(filePath);
			size = reader.getNumberOfPages();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (reader != null) {
				reader.close();
			}
		}
		return size;
	}

	private void comparePageSize(int index, int pageSize)
			throws PageIndexOutOfBoundException {
		if (index > pageSize) {
			throw new PageIndexOutOfBoundException("Page index " + index
					+ " larger than the page size " + pageSize);
		}
	}
}
